//
//  YSYBaseViewController.m
//  YSYUISateRestorationDemo
//
//  Created by muxue on 2019/9/27.
//  Copyright © 2019 暮雪. All rights reserved.
//

#import "YSYBaseViewController.h"
#import "YSYRestorationModel.h"

static NSInteger YSYBaseOrder = 0;

@interface YSYBaseViewController ()

@end

@implementation YSYBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    YSYBaseOrder += 1;
    self.restorationIdentifier = [NSString stringWithFormat:@"%@_%ld", NSStringFromClass(self.class), (long)YSYBaseOrder];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
     YSYBaseOrder += 1;
     self.restorationIdentifier = [NSString stringWithFormat:@"%@_%ld", NSStringFromClass(self.class), (long)YSYBaseOrder];
}

- (void)decodeRestorableStateWithCoder:(NSCoder *)coder {
    [super decodeRestorableStateWithCoder:coder];
    NSLog(@"[%@ decodeRestorableStateWithCoder]", NSStringFromClass(self.class));
}

- (void)encodeRestorableStateWithCoder:(NSCoder *)coder {
    [super encodeRestorableStateWithCoder:coder];
    NSLog(@"[%@ encodeRestorableStateWithCoder]", NSStringFromClass(self.class));
}

+ (YSYRestorationModel *)getRestorationModelClassWithRestorationIdentifier:(NSString *)restorationIdentifier {
    if (restorationIdentifier.length <= 0) {
        return nil;
    }
    
    NSArray *components = [restorationIdentifier componentsSeparatedByString:@"_"];
    if (components.count < 2) {
        return nil;
    }
    YSYRestorationModel *model = [[YSYRestorationModel alloc] init];
    model.classString = components.firstObject;
    model.orderNumber = [components.lastObject integerValue];
    return model;
}

@end
